package com.laboratory.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.laboratory.mapper.LabBookingMapper;
import com.laboratory.model.dto.labBooking.LabBookingProcessDTO;
import com.laboratory.model.dto.labBooking.LabBookingQueryDTO;
import com.laboratory.model.entity.LabBooking;
import com.laboratory.model.vo.labBooking.LabBookingEeVO;
import com.laboratory.model.vo.labBooking.LabBookingVO;
import com.laboratory.service.LabBookingService;
import com.laboratory.utils.SecurityHolderUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author lucky
 * @description 针对表【lab_booking】的数据库操作Service实现
 * @createDate 2024-07-18 20:11:27
 */
@Service
public class LabBookingServiceImpl extends ServiceImpl<LabBookingMapper, LabBooking>
        implements LabBookingService {

    @Override
    public boolean save(LabBooking entity) {
        entity.setUserId(SecurityHolderUtils.getUserId());
        return super.save(entity);
    }

    @Override
    public List<LabBookingVO> getAll(Integer pIndex, Integer pSize, LabBookingQueryDTO labBookingQueryDTO) {
        // 开启分页
        PageHelper.startPage(pIndex, pSize);

        // 预约时间
        String[] bookingTimes = labBookingQueryDTO.getBookingTimes();
        if (ObjectUtil.isNotEmpty(bookingTimes)) {
            labBookingQueryDTO.setStartTime(bookingTimes[0]);
            labBookingQueryDTO.setEndTime(bookingTimes[1]);
        }

        // 管理员查询所有
        if (labBookingQueryDTO.getRoleId() == 1) {
            labBookingQueryDTO.setUserId(null);
        }

        return baseMapper.getAll(labBookingQueryDTO);
    }

    @Override
    public void importData() {
        EasyExcel.write("D:\\实验室预约记录-" + DateUtil.format(new Date(), DatePattern.PURE_DATETIME_FORMAT) + ".xlsx", LabBookingEeVO.class)
                .sheet("实验室预约记录")
                .doWrite(baseMapper.getAll(null));
    }

    @Override
    public Boolean process(LabBookingProcessDTO labBookingProcessDTO) {
        LabBooking labBooking = new LabBooking();
        labBooking.setId(labBookingProcessDTO.getId());
        labBooking.setBookingStatus(labBookingProcessDTO.getBookingStatus());
        return super.updateById(labBooking);
    }
}




